import {
  getComments,
  getPosts,
  publishPost,
  removeComment,
  removePost,
  updatePost,
  updatePostStatus
} from '@/service/main/post/post'
import { defineStore } from 'pinia'
import type { IComment, IPostState } from './type'

const usePostStore = defineStore('post', {
  state: (): IPostState => ({
    postsList: [],
    postsTotalCount: 0,
    comment: [],
    postId: 0
  }),
  actions: {
    // 发布帖子
    async publishPostAction(postInfo: any) {
      await publishPost(postInfo)
    },
    // 删除帖子
    async removePostAction(post_id: number) {
      await removePost(post_id)
    },
    // 修改帖子
    async updatePostAction(post_id: number, postInfo: any) {
      await updatePost(post_id, postInfo)
    },
    // 修改帖子状态
    async updatePostStatusAction(post_id: number, status: string) {
      await updatePostStatus(post_id, status)
    },
    // 查询帖子
    async getPostsAction(queryInfo?: any) {
      const res = await getPosts(queryInfo)
      const { list, totalCount } = res.data
      this.postsList = list
      this.postsTotalCount = totalCount
    },
    // 查询评论
    async searchCommentByIdAction(post_id: number) {
      const res = await getComments(post_id)
      this.comment = res.data
      this.postId = post_id
    },
    // 删除评论
    async removeCommentByIdAction(comment_id: number) {
      const res = await removeComment(comment_id)
      this.searchCommentByIdAction(this.postId)
    }
  }
})

export default usePostStore
